/**
 * 
 */
package huajiu.com.utlis;

import huajiu.com.base.CdResult;
import huajiu.com.base.CommonConstant;
import huajiu.com.base.EmotionResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.lf5.util.StreamUtils;


/**
 * @author ItBotPjq
 *2018年1月17日
 */
public class NetHttpRrequest {
	
	public static CdResult recognizeTongue(String base64, String ext) {
		try {

			Map<String, String> map = new HashMap<String, String>();
			map.put(CommonConstant.CdSystem_APP_Key, "EMO251463APP30514286979537614028");
			map.put(CommonConstant.CdSystem_APP_Pwd, "EMO251463APP89512074635629170843");
			map.put("tongue_image", base64);
			map.put("imgfmt", "jpg");
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://api.open3z.com/tcm/tongue");

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (String key : map.keySet()) {
				BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, map.get(key));
				params.add(basicNameValuePair);
			}
			post.setEntity((new UrlEncodedFormEntity(params)));
			HttpResponse res = httpClient.execute(post);
			HttpEntity entity = res.getEntity();
			if (entity != null) {
				String json = new String(StreamUtils.getBytes(entity.getContent()), "UTF-8");
				System.out.println(json);
				CdResult cd = (CdResult) JsonResultUtil.jsonToObject(json, CdResult.class);
				return cd;
			}
		} catch (Exception e) {
			System.out.println("出错了,可能是json出错");
		}
		return null;
	}
	
	public static CdResult recognizePhysique(String base64, String ext) {
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put(CommonConstant.CdSystem_APP_Key, "EMO251463APP30514286979537614028");
			map.put(CommonConstant.CdSystem_APP_Pwd, "EMO251463APP89512074635629170843");
			map.put("image", base64);
			map.put("imgfmt", "jpg");
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://api.open3z.com/tcm/face");

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (String key : map.keySet()) {
				BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, map.get(key));
				params.add(basicNameValuePair);
			}
			post.setEntity((new UrlEncodedFormEntity(params)));
			HttpResponse res = httpClient.execute(post);
			HttpEntity entity = res.getEntity();
			if (entity != null) {
				String json = new String(StreamUtils.getBytes(entity.getContent()), "UTF-8");
				System.out.println(json);
				CdResult cd = (CdResult) JsonResultUtil.jsonToObject(json, CdResult.class);
				return cd;
			}
		} catch (Exception e) {
			System.out.println("出错了,可能是json出错");
		}	
		return null;
	}
	
	public static CdResult emotion(String base64, String ext) {
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put(CommonConstant.CdSystem_APP_Key, "EMO251463APP30514286979537614028");
			map.put(CommonConstant.CdSystem_APP_Pwd, "EMO251463APP89512074635629170843");
			map.put("image", base64);
			//map.put(,ext);
			map.put("group_name", "emotion");
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://api.open3z.com/emotion/face_emotion");

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (String key : map.keySet()) {
				BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, map.get(key));
				params.add(basicNameValuePair);
			}
			post.setEntity((new UrlEncodedFormEntity(params)));
			HttpResponse res = httpClient.execute(post);
			HttpEntity entity = res.getEntity();
			if (entity != null) {
				String json = new String(StreamUtils.getBytes(entity.getContent()), "UTF-8");
				System.out.println(json);
				CdResult er = (CdResult) JsonResultUtil.jsonToObject(json, CdResult.class);
				return er;
			}
		} catch (Exception e) {
			System.out.println("出错了,可能是json出错");
		}
		return null;
	}

}
